//
//  DAInputCommentView.m
//  DAKnowledge
//
//  Created by DreamArts on 16/6/24.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAInputCommentView.h"
#import "DAHeader.h"
#import "Masonry.h"
#import "Utility.h"

@interface DAInputCommentView () <UITextViewDelegate>


@property (nonatomic, strong) UIButton *sendButton;

// block
@property (nonatomic, copy) ContentSizeBlock sizeBlock;
@property (nonatomic, copy) replyAddBlock replyBlock;


@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *topView;

@end

@implementation DAInputCommentView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
       // 注册一个通知 用来更改placeHolder的值
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changePlaceHolder:) name:@"CHANGEPLACEHOLDER" object:nil];
        [self confignView];
    }
    return self;
}

- (void)confignView{
    self.backView = [[UIView alloc]init];
    self.backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.backView];
    
    self.sendTextView = [[UITextView alloc]init];
    self.sendTextView.backgroundColor = [UIColor colorWithHexString:timeLineColor];
    self.sendTextView.layer.cornerRadius = 5;
    self.sendTextView.delegate = self;
   // self.sendTextView.returnKeyType = UIReturnKeySend;
    [self.backView addSubview:self.sendTextView];
    
    NSString *comment = NSLocalizedString(@"comment", nil);
    self.placeHolder = [[UILabel alloc]init];
    self.placeHolder.text = comment;
    self.placeHolder.textColor = [UIColor lightGrayColor];
    self.placeHolder.font = [UIFont systemFontOfSize:12];
    [self.backView addSubview:self.placeHolder];
    
    NSString *sendMessage = NSLocalizedString(@"sendMessage", nil);
    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendButton setTitle:sendMessage forState:UIControlStateNormal];
    [self.sendButton setTitleColor:[UIColor colorWithHexString:borrowTimeColor]  forState:UIControlStateNormal];
    [self.sendButton addTarget:self action:@selector(clickSendButton:) forControlEvents:UIControlEventTouchUpInside];
    self.sendButton.tag = 10001;
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.sendButton.userInteractionEnabled = NO;
    [self.backView addSubview:self.sendButton];
}

- (void)layoutSubviews{
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(self).offset(0);
    }];
    
    [self.sendTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(SCREENWIDTH - 80);
    }];
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
       // make.top.mas_equalTo(5);
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
        make.bottom.mas_equalTo(-5);
    }];

    [self.placeHolder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(13);
        make.width.mas_equalTo(self.sendTextView);
        make.bottom.mas_equalTo(-10);
    }];
    
}

- (void)clickSendButton:(UIButton *)sender{
    NSLog(@"发送评论");
    self.replyBlock(self.sendTextView.text, sender.tag);
    self.sendButton.userInteractionEnabled = NO;
    [self.sendButton setTitleColor:[UIColor colorWithHexString:borrowTimeColor]  forState:UIControlStateNormal];
}
#pragma mark - tapView的手势方法
- (void)disapper{
    [self.sendTextView resignFirstResponder];
    [self.sendTextView removeFromSuperview];
    self.topView = nil;
    [self removeFromSuperview];
} 
- (void)setContentSizeBlock:(ContentSizeBlock)block{
    self.sizeBlock = block;
}
- (void)setReplyAddBlock:(replyAddBlock)block{
    self.replyBlock = block;
}

#pragma mark -通知触发的方法
- (void)changePlaceHolder:(NSNotification *)notif{
    NSString *replayName = notif.object;
    self.placeHolder.hidden = NO;
    NSString *reply = NSLocalizedString(@"reply", nil);
    self.placeHolder.text = [NSString stringWithFormat:@"%@%@:", reply,replayName];
}
#pragma mark -textView delegte
- (void)textViewDidChange:(UITextView *)textView{
    self.placeHolder.hidden = (textView.text.length > 0);
    CGSize contentSize = self.sendTextView.contentSize;
    if (textView.text.length > 0) {
        [self.sendButton setTitleColor:[UIColor supportGreenColor] forState:UIControlStateNormal];
        self.sendButton.userInteractionEnabled = YES;
    }else{
        [self.sendButton setTitleColor:[UIColor colorWithHexString:borrowTimeColor] forState:UIControlStateNormal];
        self.sendButton.userInteractionEnabled = NO;
    }
    self.sizeBlock(contentSize);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
